package com.qianfeng.qlm.handforireader.activity;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.media.Image;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.qianfeng.qlm.handforireader.R;
import com.qianfeng.qlm.handforireader.adapter.NewContentRecycleAdapter;
import com.qianfeng.qlm.handforireader.model.Category;
import com.qianfeng.qlm.handforireader.model.NewContentBean;
import com.qianfeng.qlm.handforireader.model.NewContentBeanList;

import org.json.JSONObject;

import java.util.ArrayList;

public class ContentListActivity extends ActionBarActivity {

    private RecyclerView recyclerView;
    private String url;
    private NewContentBeanList contentBeanList;
    private NewContentRecycleAdapter adapter;
    private ImageView imgLoading;
    private RelativeLayout rlLoading;
    private AnimationDrawable animationDrawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content_list_view);
        recyclerView = (RecyclerView) findViewById(R.id.recyler_view);
        LinearLayoutManager layout = new LinearLayoutManager(this);
        layout.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layout);
        ArrayList<NewContentBean> data = new ArrayList<>();
        adapter = new NewContentRecycleAdapter(this, data);
        recyclerView.setAdapter(adapter);

        rlLoading = (RelativeLayout) findViewById(R.id.rl_loading);
        imgLoading = (ImageView)findViewById(R.id.content_loading_img);

        Intent intent = getIntent();
        if (intent != null){
            url = intent.getStringExtra("url");
            initRequest();
        }

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        Intent nameIntent = getIntent();
        String name = nameIntent.getStringExtra("name");
        actionBar.setTitle(name);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId){
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initRequest(){
        RequestQueue requestQueue = Volley.newRequestQueue(this);

        JsonObjectRequest request = new JsonObjectRequest(
                url,
                null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {

                        if (response != null){
                            animationDrawable.stop();
                            rlLoading.setVisibility(View.GONE);
                            contentBeanList = new NewContentBeanList();
                            contentBeanList.parseJson(response);
                            if (contentBeanList.getData() != null){
                                adapter.setData(contentBeanList.getData());
                            }
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }
        );

        animationDrawable = (AnimationDrawable) imgLoading.getDrawable();
        animationDrawable.start();
        requestQueue.add(request);
    }


}
